package com.cincc.cinccimdemo.common;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.transition.Transition;

public class GlideUtils {

    public static void initImageWithFile(Context context, String url, ImageView imageView, int corners) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .into(imageView);
    }
    public static void initImageWithFileCacheCorners(Context context, String url, ImageView imageView, int corners) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(corners)))
                .into(imageView);
    }

    public static void initImageWithFileCache(Context context, String url, ImageView imageView, int defaultImage) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .placeholder(defaultImage)
                .centerCrop()
                .into(imageView);
    }

    public static void initImageWithFileCacheAsBitmap(Context context, String url, ImageView imageView) {
        Glide.with(context).asBitmap()
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .into(imageView);
    }

    public static void initCircleImageWithFileCache(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }

    public static void initCircleImageWithFileCache(Context context, String url, ImageView imageView, int defaultImage) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defaultImage)
                .dontAnimate()
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }

    public static void initImageNoCache(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(true)
                .dontAnimate()
                .into(imageView);
    }

    public static void initImageWithFileCacheWithProgressCorners(Context context, String url, ImageView imageView, ProgressBar progressBar, int corners) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(corners)))
                .into(getImageViewTarget(imageView, progressBar));
    }

    public static void initImageWithFileCacheWithProgress(Context context, String url, ImageView imageView, ProgressBar progressBar, int corners) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .into(getImageViewTarget(imageView, progressBar));
    }

    private static ImageViewTarget<Drawable> getImageViewTarget(final ImageView imageView, final ProgressBar progressBar) {
        ImageViewTarget<Drawable> imageViewTarget = new ImageViewTarget<Drawable>(imageView) {
            @Override
            public void onLoadStarted(@Nullable Drawable placeholder) {
                super.onLoadStarted(placeholder);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                super.onResourceReady(resource, transition);
                // 图片加载完成
                progressBar.setVisibility(View.INVISIBLE);
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void setResource(@Nullable Drawable resource) {

            }
        };
        return imageViewTarget;
    }

    public static void clearMemoryCache(Context context) {
        Glide.get(context).clearMemory();
    }

    public static void clearFileCache(Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();
            }
        }).start();
    }
}
